$(function(){
	$(".main .menu li ").click(function(){
		$(this).addClass('on').siblings().removeClass('on');
		var index = $(this).index();
		if(index == '0'){
			$('.mobile').show();
			$('.email').hide();
		}else if(index == '1'){
			$('.mobile').hide();
			$('.email').show();
		}
	});
});

//检查个人普通用户注册登录
function checkEmailReg(oForm){
	var username = str_trim($(oForm).find('#username').val());
	if(username == ''){
		layerMsg('用户名不能为空!', 2);
		$(oForm).find('#username').focus();
		$(oForm).find('#username').val(username);
		return false;
	}else{
		$(oForm).find('#username').val(username);
		var regucn =/^(?=[\u4e00-\u9fa5])(?!\d+)[\u4e00-\u9fa5\w.]{2,20}$/;
		var reguen =/^(?=[\u4e00-\u9fa5a-zA-Z])(?!\d+)[\u4e00-\u9fa5\w.]{6,20}$/;
		if(!regucn.test(username)){
			if(!reguen.test(username)){
				layerMsg('中英文开头6-20位，无特殊符号!', 2);
				$(oForm).find('#username').focus();
				return false;
			}
		}
	}

	var email = str_trim($(oForm).find('#email').val());
	if(email == ''){
		layerMsg('请输入邮箱地址!', 2);
		$(oForm).find('#email').focus();
		$(oForm).find('#email').val(email);
		return false;
	}else{
		var regu = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		if(!regu.test(email)){
			layerMsg('请输入正确的邮箱地址!', 2);
			$(oForm).find('#email').focus();
			$(oForm).find('#email').val(email);
			return false;
		}
	}
	//密码
	if($(oForm).find('#password').val() == '' || $(oForm).find('#password').val().length < 6){
		layerMsg('密码不能为空!', 2);
		$(oForm).find('#password').focus();
		return false;
	}
	//确认密码
	if($(oForm).find('#password').val() != $(oForm).find('#password_confirmation').val()){
		layerMsg('确认密码必须和新密码一致!', 2);
		$("#password_confirmation").focus();
		return false;
	}
	var code = str_trim($(oForm).find('#code').val());
	if(code == ''){
		layerMsg('请输入验证码!', 2);
		$(oForm).find('#code').focus();
		$(oForm).find('#code').val(code);
		return false;
	}else{
		$.post('/common/ajax_code', {'_token': _token, 'code': code}, function(data){
			if(data == "true"){
				//同意注册
				if(!$(oForm).find("input[type='checkbox']").is(':checked')){
					layerMsg('请同意注册协议!', 2);
					return false;
				}
				var queryString = $(oForm).serialize();
				$.ajax({
					url: '/mobile/personal/register',
					dataType: "json",
					data: queryString,
					type: "POST",
					beforeSend: function(){
						$(oForm).find("input[type='submit']").val('请稍候...');
						$(oForm).find("input[type='submit']").attr('disabled', true);
					},
					success: function(data){
						if(data.status == '0'){
							layerMsg(data.msg, 2);
							location = data.redirectPath;
						}else if(data.status == '3'){
							layerMsg(data.msg, 3);
							$(oForm).find("input[type='submit']").val('立即注册');
							$(oForm).find("input[type='submit']").removeAttr('disabled');
						}else{
							layerMsg('可能网络原因引起的错误！', 2);
							$(oForm).find("input[type='submit']").val('立即注册');
							$(oForm).find("input[type='submit']").removeAttr('disabled');
						}
					},
					error: function(data){
						var msgErr = '';
						var errors = $.parseJSON(data.responseText);
						$.each(errors, function(index, value){
							msgErr += "<p style='line-height: .4rem; width: 100%'>" + value + ";</p>";
						});
						layerMsg(msgErr, 2);
						$(oForm).find("input[type='submit']").val('立即注册');
						$(oForm).find("input[type='submit']").removeAttr('disabled');
					},
					complete: function(){
						$(oForm).find('#code').val('');
						$(oForm).find('#getcode').attr('src',imgCode + Math.random());
					}
				});
			}else{
				layerMsg('验证码错误!', 2);
				$(oForm).find('#code').focus();
				$(oForm).find('#code').val(code);
				return false;
			}
		});
	}
	return false;
}

//检查个人手机用户注册登录
function checkMobileReg(oForm){
	var mobile = str_trim($(oForm).find('#mobile').val());
	if(mobile == ''){
		layerMsg('请输入手机号码!', 2);
		$(oForm).find('#mobile').focus();
		$(oForm).find('#mobile').val(mobile);
		return false;
	}else{
		var regu = /(^1[3-9]{1}[0-9]{9}$)/;
		if(!regu.test(mobile)){
			layerMsg('请输入正确的手机号码!', 2);
			$(oForm).find('#mobile').focus();
			$(oForm).find('#mobile').val(mobile);
			return false;
		}
	}

	var mobCode = str_trim($(oForm).find('#mobCode').val());
	if(mobCode == ''){
		layerMsg('请输入验证码!', 2);
		$(oForm).find('#mobCode').focus();
		$(oForm).find('#mobCode').val(mobCode);
		return false;
	}else{
		$.post('/mobile/personal/ajax_sms_url', {
			'_token': _token,
			'mobile': mobile,
			'mobCode': mobCode
		}, function(data){
			if(data == "true"){
				//密码
				if($(oForm).find('#password').val() == '' || $(oForm).find('#password').val().length < 6){
					layerMsg('密码不能少于6位!', 2);
					$(oForm).find('#password').focus();
					return false;
				}
				//确认密码
				if($(oForm).find('#password').val() != $(oForm).find('#password_confirmation').val()){
					layerMsg('确认密码必须和新密码一致!', 2);
					$("#password_confirmation").focus();
					return false;
				}
				//同意注册
				if(!$(oForm).find("input[type='checkbox']").is(':checked')){
					layerMsg('请同意注册协议!', 2);
					return false;
				}

				var queryString = $(oForm).serialize();
				$.ajax({
					url: '/mobile/personal/register',
					dataType: "json",
					data: queryString,
					type: "POST",
					beforeSend: function(){
						$(oForm).find("input[type='submit']").val('请稍候...');
						$(oForm).find("input[type='submit']").attr('disabled', true);
					},
					success: function(data){
						if(data.status == '0'){
							layerMsg(data.msg, 2);
							location = data.redirectPath;
						}else{
							$(oForm).find("input[type='submit']").val('保存');
							$(oForm).find("input[type='submit']").removeAttr('disabled');
							layerMsg('可能网络原因引起的错误！', 2);
						}
					},
					error: function(data){
						var msgErr = '';
						var errors = $.parseJSON(data.responseText);
						$.each(errors, function(index, value){
							msgErr += "<p style='line-height: .4rem; width: 100%'>" + value + ";</p>";
						});
						layerMsg(msgErr, 2);
						$(oForm).find("input[type='submit']").val('保存');
						$(oForm).find("input[type='submit']").removeAttr('disabled');
					},
					complete: function(){
					}
				});

			}else{
				layerMsg('验证码和手机不匹配!', 2);
				$(oForm).find('#mobCode').focus();
				$(oForm).find('#mobCode').val(mobCode);
				return false;
			}
		});
	}
	return false;
}

//点击获取手机验证码
var mobTime;
function getModCode(othis, objMobile){
	var mobile = str_trim($(objMobile).val());
	if(mobile != ''){
		var mobValue = str_trim(mobile);
		var regu = /(^1[3-9]{1}[0-9]{9}$)/;
		if(regu.test(mobValue)){
			$.post('/mobile/personal/ajax_mobile', {'_token': _token, 'mobile': mobile}, function(data){
				var src = 'this.src="' + imgCode + '" + Math.random()';
				var docSrc = 'document.getElementById("imgCodes").src="' + imgCode + '" + Math.random()';
				var str = "";
				str += "<div class='addCode'>";
				str += "<form method='post' name='mForm' id='mForm' onsubmit='return false'>";
				str += "<span><input name='code' id='code' type='text'  placeholder='验证码' class='code' ></span>";
				str += "<span><img id='imgCodes' src='" + imgCode + Math.random() + "' onclick='" + src + "' /></span>";
				str += "<span class='change'><a onclick='" + docSrc + "'>换一换</a></span>";
				str += "<div class='codeErr' id='codeErr'></div>";
				str += "</form>";
				str += "</div>";

				layer.open({
					title: [
						'请输入验证码',
						'background-color:#00c6c9; color:#fff; height:.8rem; line-height:.8rem'
					]
					, className: 'loginCode' //自定风格
					, content: str
					, shadeClose: false
					, style: 'font-family:微软雅黑; font-size:.28rem'
					, btn: ['确认', '取消']
					, yes: function(index){
						var code = $("#mForm").find("#code").val();
						$.post('/common/ajax_code', {'_token': _token, 'code': code}, function(data){
							if(data == 'true'){
								$.post('/mobile/personal/ajax_mob_code', {
									'_token': _token,
									'mobile': mobValue,
									'code': code
								}, function(data){
									if(data.status == 2){
										//验证手机号码是不是存在
										//layerMsg(data.msg, 2);.
										layer.open({
											title: [
												'该手机号码已经被注册,请选择操作方式',
												'background-color: #FF4351; color:#fff;'
											]
											, shadeClose: false
											, className: 'layer-btn' //自定风格
											, content: '<div class="layui-layer-btn">' +
											'<a class="layui-layer-btn0"  href="/mobile/personal/find_password?mobile=' + mobValue + '">找回密码</a>' +
											'<a class="layui-layer-btn1" href="/mobile/personal/login?mobile=' + mobValue + '">动态登录</a>' +
											'<a class="layui-layer-btn2" onclick="carryon()">继续注册</a>' +
											'</div>'
										});
									}else if(data.status == 1){
										//获取验证码成功
										layerMsg(data.msg, 2);
										var iNum = 60;
										mobTime = setInterval(function(){
											iNum -= 1;
											othis.attr("disabled", true).val('(' + iNum + ')秒后重新获取').css({
												"background": "#ccc",
												"color": "#333"
											});
											if(iNum <= 0){
												othis.removeAttr("disabled").val('获取验证码').css({
													"background": "#505962",
													"color": "#fff"
												});
												clearInterval(mobTime);
											}
										}, 1000);
									}else if(data.status == 0){
										//验证码获取失败
										layerMsg(data.msg, 2);
									}
								});
							}else{
								$("#mForm").find("#codeErr").html('验证码错误');
							}
						});
					}
				});
			});
		}else{
			objMobile.val(str_trim(mobile));
			layerMsg('请输入正确的手机号码!', 2);
		}
	}else{
		objMobile.val(str_trim(mobile));
		layerMsg('请填写手机号码!', 2);
	}
}
/**
 * 手机号码注册
 */
function carryon(){
	layerMsg('手机号码已经注册，请重新输入', 2);
	$("#mobileForm").find("#mobile").focus();
}
